import * as THREE from "three"
import { color } from "../config"
import surroundLineVertex from "../shaders/surroundLine/vertex.glsl"
import surroundLineFrag from "../shaders/surroundLine/fragment.glsl"
export class SurroundLine {
    scene
    child
    size
    up_height: THREE.IUniform
    move_line: THREE.IUniform
    constructor(scene: THREE.Scene, child: THREE.Mesh, up_height: THREE.IUniform, move_line: THREE.IUniform) {
        this.scene = scene
        this.child = child
        this.size = 100
        this.up_height = up_height
        this.move_line = move_line

        this.renderMesh()
        this.rendersoundLine()
    }

    computedHeight() {
        this.child.geometry.computeBoundingBox()
        this.child.geometry.computeBoundingSphere()
    }
    renderMesh() {
        this.computedHeight()
        const { max, min } = this.child.geometry.boundingBox as THREE.Box3
        const size = (max.z - min.z)

        const material = new THREE.ShaderMaterial({
            vertexShader: surroundLineVertex,
            fragmentShader: surroundLineFrag,
            uniforms: {
                //横向扫描线
                move_line: this.move_line,
                move_line_section: {
                    value: 200
                },
                u_move_line_color: {
                    value: new THREE.Color(color.liveColor)
                },
                // 上升扫描线
                up_height: this.up_height,
                up_height_section: {
                    value: 10
                },
                u_up_color: {
                    value: new THREE.Color(color.risingColor)
                },
                //基础色
                city_color: {
                    value: new THREE.Color(color.mesh)
                },
                // 高处色
                u_head_color: {
                    // 要有一个头部颜色 最顶部显示的颜色
                    value: new THREE.Color(color.head)
                },
                // 最大高度差
                u_size: {
                    value: size
                },
                // 表框线颜色
                u_soundLine: {
                    value: new THREE.Color(color.soundLine)
                }
            },
        })
        // const material=new THREE.MeshLambertMaterial({color:0x1b3045})
        const mesh = new THREE.Mesh(this.child.geometry, material)
        //复制原来child的位置，缩放，旋转
        this.copyInfo(mesh, this.child)
        this.scene.add(mesh)
    }
    rendersoundLine() {
        const geometry = new THREE.EdgesGeometry(this.child.geometry)
        const material = new THREE.LineBasicMaterial({ color: color.soundLine })
        const line = new THREE.LineSegments(geometry, material)

        this.copyInfo(line, this.child)

        this.scene.add(line)
    }
    copyInfo(newMesh: THREE.Object3D, oldMesh: THREE.Object3D) {
        newMesh.position.copy(oldMesh.position)
        newMesh.rotation.copy(oldMesh.rotation)
        newMesh.scale.copy(oldMesh.scale)
    }
}